//
//  ByShopListVC.m
//  hutouben_ios
//
//  Created by pro on 16/11/29.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByShopListVC.h"

// view
#import "DOPDropDownMenu.h"

// cell
#import "ByShopListProtuctCell.h"

// vc
#import "ByShopProductDeatilVC.h"   // 详情
#import "ByShopCarVC.h"             // 购物车

@interface ByShopListVC () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, DOPDropDownMenuDataSource,DOPDropDownMenuDelegate>

// view
@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, strong) DOPDropDownMenu  *menu;           // 菜单

@end

@implementation ByShopListVC
{
    NSArray *_sorts;        // 排序
    NSArray *_filters;      // 筛选
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"商城列表";
    
    [self addLeftBackBarButtonItem];
    [self addBYItemviews:@[@"购物车"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeRight];
    
    [self createMenu];
    [self createCollectionView];
}

#pragma mark - create view
- (void)createMenu {
    
    _filters = @[@"0-1000", @"1000-2000", @"2000-3000"];
    _sorts = @[@"默认排序", @"价格从低到高", @"价格从高到低"];

    // 添加下拉菜单
    self.menu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, 0) andHeight:44];
    self.menu.delegate = self;
    self.menu.dataSource = self;
    [self.view addSubview:_menu];
    
}
- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    CGFloat width = (SCREEN_WIDTH - 10 * 3) / 2;
    CGFloat height = width + 25 + 30 + 30;  // 图片+标题(25)+描述(30)+价格(20)
    layout.itemSize = CGSizeMake(width, height);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.menu.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - self.menu.height) collectionViewLayout:layout];
    self.collection.backgroundColor = TABLE_COLOR;
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.view addSubview:self.collection];
    [self.collection registerClass:[ByShopListProtuctCell class] forCellWithReuseIdentifier:@"ByShopListProtuctCell"];
    
}

#pragma mark - UICollectionViewDatasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ByShopListProtuctCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ByShopListProtuctCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor orangeColor];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ByShopProductDeatilVC *vc = [[ByShopProductDeatilVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}

#pragma mark - menu delegate
- (NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu
{
    return 2;
}

- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column {
    if (column == 0) {
        return _sorts.count;
    } else {
        return _filters.count;
    }
}

- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath {
    if (indexPath.column == 0) {
        return _sorts[indexPath.row];
    } else {
        return _filters[indexPath.row];
    }
}

- (void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath {


}



#pragma mark - 点击事件
- (void)rightItemsAction:(NSInteger)rightIndex {
    
    ByShopCarVC *vc = [[ByShopCarVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}

@end
